home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13703 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: solon.com!not-for-mail
  2. From: kanze@gabi-soft.fr (J. Kanze)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated,hp.unix,comp.sys.hp.apps,comp.sys.hp.hpux
  4. Subject: Re: C coding problem
  5. Date: 9 Apr 1996 17:39:35 -0500
  6. Organization: GABI Software, Sarl.
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4keov7$8bk@solutions.solon.com>
  10. References: <4j06na$808@solutions.solon.com> <4jttan$3gf@solutions.solon.com>
  11.     <4jv6st$crf@solutions.solon.com> <4k1qh3$5hn@solutions.solon.com>
  12. NNTP-Posting-Host: solutions.solon.com
  13.  
  14. In article <4k1qh3$5hn@solutions.solon.com> news@Thinkage.On.CA writes:
  15.  
  16. |> In article <4jv6st$crf@solutions.solon.com> schwarz@mips.complang.tuwien.ac.at (Konrad Schwarz) writes:
  17. |> >
  18. |> >|> a[i] is converted to *(a + i) by the compiler in any case so
  19. |> >|> whats the advantage with your approach?
  20. |> >
  21. |> >The machine doesn't have to add, the machine doesn't have to multiply,
  22. |> >*a is less complicated to understand than a [i] since only one variable
  23. |> >is involved, *a is more type safe than a [i], since there is nothing
  24. |> >constraining i (besides it being of integral type), the optimizer
  25. |> >has less to do, compile time decreases, etc. 
  26.  
  27. |> However it is not always so that the machine does a distinct
  28. |> add or multiply in order to handle a[i].  The "*p++" idiom may look
  29. |> shot in the C code, and on a PDP-11 it usually resulted in nice
  30. |> short code.  However, there is no guarentee that this is true of all
  31. |> hardware.  It is not all that uncommon to have an architecture where
  32. |> adding 1 to an int is much cheaper than adding one to a pointer,
  33. |> In such and environment, 
  34. |>       *q++ = *p++;
  35. |> loses badly to
  36. |>       q[i] = p[i], ++i;
  37. |> if the hardware indexing works in terms of the objects being
  38. |> referenced.
  39.  
  40. *Normally*, any decent compiler will convert either of these idioms to
  41. whichever is fastest on that machine, although I do have memories of a
  42. Microsoft C compiler which converted the array notation to pointers,
  43. even though on the target machine (8086, large model), it could keep the
  44. (single) index in memory, but not the pointers.  (Turning off
  45. optimization resulted in a program that ran faster and was smaller.)
  46.  
  47. >From a compiler point of view, the array notation will normally result
  48. in better optmization.  Sure, it gets converted into pointer arithmetic.
  49. But since the compiler generates all of the operations on the pointers,
  50. it knows the full constraints on them.  In practice, I doubt that this
  51. will make a significant difference, however.
  52.  
  53. |> >Why use C if you're uncomfortable with pointer arithmetic?  
  54.  
  55. Because that is the compiler that is available:-).
  56.  
  57. |> I don't think the original poster was uncomfortable with pointers.
  58.  
  59. |> There is a persistent folklore that says just because you code
  60. |> something with pointers it is magically faster.  It is not true. 
  61. |> Sometimes using a pointers is faster, sometimes using subscripting is
  62. |> faster.  Usually it makes MINISCULE difference.
  63.  
  64. |> The converse folklore is that subscripting is always more readable.
  65. |> It is also untrue.  Pointer notation can greatly reduce program
  66. |> clutter so you can see what is happening.
  67.  
  68. What is obfuscation is using pointers when the original data is an array
  69. (or vice-versa).  (Another obfuscation is writing a loop when there is a
  70. standard function which does the same thing.  The reader will probably
  71. spend a significant amount of time trying to figure out why you didn't
  72. use the function, and what you are doing differently.)
  73. -- 
  74. James Kanze           (+33) 88 14 49 00          email: kanze@gabi-soft.fr
  75. GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
  76. Conseils en informatique industrielle --
  77.                             -- Beratung in industrieller Datenverarbeitung
  78.